package com.chatifyme;

import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Presence;
 
import android.os.AsyncTask;
import android.os.Handler;
import android.util.Log;

public class NetworkConnection{
	
	//private XMPPClient xmppClient;
	public static XMPPConnection xmppConnection;
	public Handler mHandler = new Handler();
	public static ConnectionModel connectionConfig = null;
	 
	public NetworkConnection(ConnectionModel config )
	{
		this(config, false);
	}
	
	public NetworkConnection(ConnectionModel config, boolean openConnection){
		 
		NetworkConnection.connectionConfig = config;
		if ( openConnection)
		{
			if ( NetworkConnection.connectionConfig != null )
			{
			    ConnectionConfiguration connectioncnfg =
			            new ConnectionConfiguration(NetworkConnection.connectionConfig.hostName, 
			            		NetworkConnection.connectionConfig.portNumber,
			            		NetworkConnection.connectionConfig.serviceName);
			    
			    xmppConnection = new XMPPConnection(connectioncnfg);
			     try {
					xmppConnection.login(connectionConfig.userName, connectionConfig.password);
				} catch (XMPPException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
	    	//new ConnectAsyncAction().execute();
		}
		
	}
	
	    
	public void Login()
	{

	    try {
	    	
	    	NetworkConnection.xmppConnection.login(NetworkConnection.connectionConfig.userName, 
	    			NetworkConnection.connectionConfig.password);
	    	
	        Log.i("XMPPClient", "Logged in as " + NetworkConnection.xmppConnection.getUser());

	        // Set the status to available
	        Presence presence = new Presence(Presence.Type.available);
	        NetworkConnection.xmppConnection.sendPacket(presence);
	     
	    } catch (XMPPException ex) {
	        Log.e("XMPPClient", "[SettingsDialog] Failed to log in as " + NetworkConnection.connectionConfig.userName);
	        Log.e("XMPPClient", ex.toString());
	        NetworkConnection.xmppConnection=null;
	        
	    }
	}
	
	private class ConnectAsyncAction extends AsyncTask<Void, Void, Void> {
		@Override
	    protected Void doInBackground(Void... args) { 
			 Log.i("I am in ","i");
	    	if ( NetworkConnection.connectionConfig != null )
			{
			    ConnectionConfiguration connectioncnfg =
			            new ConnectionConfiguration(NetworkConnection.connectionConfig.hostName, 
			            		NetworkConnection.connectionConfig.portNumber,
			            		NetworkConnection.connectionConfig.serviceName);
			    
			    xmppConnection = new XMPPConnection(connectioncnfg);
			     
			  
			}
			return null;
	    }
 

	    // This is called each time you call publishProgress()
		@Override
		protected void onProgressUpdate(Void... progress) {
	       
	    }

	    // This is called when doInBackground() is finished
		@Override
		protected void onPostExecute(Void result) {
        
    }
	}
}

